package com.L.yygh.hosp.service.impl;

import com.L.yygh.cmn.client.DictFeignClient;
import com.L.yygh.hosp.repository.HospitalRepository;
import com.L.yygh.hosp.service.HospitalService;
import com.L.yygh.model.hosp.Hospital;
import com.L.yygh.vo.hosp.HospitalQueryVo;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class HospitalServiceImpl implements HospitalService {
    @Autowired
    private HospitalRepository hospitalRepository;

    @Autowired
    private DictFeignClient dictFeignClient;

    @Override
    public Hospital getByHoscode(String hoscode) {
        return hospitalRepository.getHospitalByHoscode(hoscode);
    }

    @Override
    public void save(Map<String, Object> paramMap) {
        //把参数map集合转换对象 Hospital
        String mapString = JSONObject.toJSONString(paramMap);
        Hospital hospital = JSONObject.parseObject(mapString, Hospital.class);

        //判断是否存在数据
        String hoscode = hospital.getHoscode();
        Hospital hospital1 = hospitalRepository.getHospitalByHoscode(hoscode);

        //如果存在，进行修改
        if(hospital1 != null){
            hospital1.setHosname(hospital.getHosname());
//            hospital1.setStatus(hospital.getStatus());
            hospital1.setUpdateTime(new Date());
            hospital1.setIsDeleted(0);
            hospitalRepository.save(hospital1);
        }else{
            //如果不存在，进行添加
            hospital.setStatus(0);
            hospital.setCreateTime(new Date());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        }

    }

    @Override
    public Page<Hospital> selectHsopPage(Integer currentPage, Integer limitPage, HospitalQueryVo hospitalQueryVo) {
        //创建Pageable对象
        Pageable pageable = PageRequest.of(currentPage-1,limitPage);
        //创建条件匹配器
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
        //HospitalQueryVo转换成Hospital对象
        Hospital hospital = new Hospital();
        BeanUtils.copyProperties(hospitalQueryVo,hospital);

        //创建Example对象
        Example<Hospital> example = Example.of(hospital,matcher);
        //调用方法实现查询操作
        Page<Hospital> all = hospitalRepository.findAll(example, pageable);

        //获取查询的list集合，遍历进行医院等级的封装
        all.getContent().stream().forEach(item -> {
            this.setHospitalHosType(item);
        });
        return all;


    }

    @Override
    public void updateStatus(String id, Integer status) {
        Hospital hospital = hospitalRepository.findById(id).get();
        hospital.setStatus(status);
        hospital.setUpdateTime(new Date());
        hospitalRepository.save(hospital);
    }

    @Override
    public Map<String,Object> getHospById(String id) {
        Hospital hospital = hospitalRepository.findById(id).get();
        this.setHospitalHosType(hospital);
        Map<String,Object> result = new HashMap<>();

        //医院基本信息（包含医院等级）---》更直观
        result.put("hospital",hospital);
        result.put("bookingRule",hospital.getBookingRule());
        hospital.setBookingRule(null);

        return result;
    }

    @Override
    public String getHospName(String hoscode) {
        Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);
        if(hospital!=null){
            return hospital.getHosname();
        }else{
            return null;
        }


    }

    @Override
    public List<Hospital> findByHosName(String hosname) {
        return hospitalRepository.findHospitalByHosnameLike(hosname);
    }

    @Override
    public Map<String, Object> item(String hoscode) {
        Map<String, Object> result = new HashMap<>();
        //医院详情
        Hospital hospital = this.getByHoscode(hoscode);
        this.setHospitalHosType(hospital);
        result.put("hospital", hospital);
        //预约规则
        result.put("bookingRule", hospital.getBookingRule());
        //不需要重复返回
        hospital.setBookingRule(null);
        return result;
    }

    private void setHospitalHosType(Hospital item) {

        String hostypeString = dictFeignClient.getName("Hostype", item.getHostype());
        //查询省 市 地区
        String provinceString = dictFeignClient.getName(item.getProvinceCode());
        String cityString = dictFeignClient.getName(item.getCityCode());
        String districtString = dictFeignClient.getName(item.getDistrictCode());

        item.getParam().put("fullAddress",provinceString+cityString+districtString);
        item.getParam().put("hostypeString",hostypeString);

    }
}
